package com.oron3.bouncer.interaction;

import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.View.OnTouchListener;

public class AndroidInteractionInterpreter extends InteractionInterpreter implements OnTouchListener {
	SimpleOnGestureListener mSimpleOnGestureListener;
	private GestureDetectorCompat mGestureDetector;
	private ScaleGestureDetector mScaleGestureDetector;
	private boolean mIsScrolling;

	private boolean mIsFlinging;

	protected boolean mIsZooming;
	private GestureDetectorCompat mGestureDetector2;
	public AndroidInteractionInterpreter(final Context ctx,final Repainter rep) {
		super(rep);
		mGestureDetector = new GestureDetectorCompat(ctx,new SimpleOnGestureListener() {
			private final com.oron3.bouncer.interaction.MotionEvent motionCache = new com.oron3.bouncer.interaction.MotionEvent(AndroidInteractionInterpreter.this);

			@Override
			public boolean onDoubleTap(final MotionEvent e) {
				onDoubleClick(new ClickEvent(AndroidInteractionInterpreter.this,(int)e.getX(), (int)e.getY()));
				return true;
			}
			@Override
			public boolean onScroll(final MotionEvent e1, final MotionEvent e2,
					final float distanceX, final float distanceY) {
				if (mIsFlinging || mIsZooming) 
					return true;
				mIsScrolling = true;

				motionCache.set((int)e2.getX(),(int)e2.getY(),(int)distanceX,(int)distanceY);
				if (distanceX!=0 || distanceY!=0)
					onDrag(motionCache);

				return true;
			}

			@Override
			public boolean onSingleTapConfirmed(final MotionEvent e) {
				return false;
			}
			@Override
			public boolean onFling(final MotionEvent e1, final MotionEvent e2,
					final float velocityX, final float velocityY) {
				if (mIsZooming) 
					return false;
				mIsFlinging = true;
				motionCache.setVelocity(velocityX);
				onDrag(motionCache);
				return true;
			}
		});
		mScaleGestureDetector = new ScaleGestureDetector(ctx, new ScaleGestureDetector.SimpleOnScaleGestureListener(){
			private final ZoomEvent zoomCache = new ZoomEvent(AndroidInteractionInterpreter.this,1);
			@Override
			public boolean onScale(final ScaleGestureDetector detector) {
				mIsZooming = true;
				zoomCache.setZoomFactor(1/detector.getScaleFactor());
				onZoom(zoomCache);
				return true;
			}
		});
		mGestureDetector2 = new GestureDetectorCompat(ctx,new OnGestureListener() {

			@Override
			public boolean onDown(final MotionEvent e) {
				onPress(new InteractiveEvent(AndroidInteractionInterpreter.this,(int) e.getX(), (int) e.getY()));
				return true;
			}
			@Override
			public void onLongPress(final MotionEvent e) {
			}
			@Override
			public void onShowPress(final MotionEvent e) {
			}
			@Override
			public boolean onSingleTapUp(final MotionEvent e) {
				return false;
			}
			@Override
			public boolean onFling(final MotionEvent e1, final MotionEvent e2,
					final float velocityX, final float velocityY) {
				return false;
			}
			@Override
			public boolean onScroll(final MotionEvent e1, final MotionEvent e2,
					final float distanceX, final float distanceY) {
				return false;
			}

		});
	}

	@Override
	public boolean onTouch(final View v, final MotionEvent event) {
		boolean retVal = mScaleGestureDetector.onTouchEvent(event);
		retVal = mGestureDetector.onTouchEvent(event) || retVal;
		retVal = mGestureDetector2.onTouchEvent(event) || retVal;
		if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
			mIsFlinging = mIsZooming = false;
			if (mIsScrolling ) {
				mIsScrolling  = false;
				onDragEnd();
			};
			onRelease(new InteractiveEvent(AndroidInteractionInterpreter.this,(int) event.getX(), (int) event.getY()) );
		}


		return retVal;
	}






}
